package com.mvvm.ktnet.base.myappbase

import android.graphics.drawable.ColorDrawable
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import com.kingja.loadsir.core.LoadService
import com.mvvm.ktnet.R
import com.mvvm.ktnet.base.fragment.IBaseVmDbFragment
import com.mvvm.ktnet.base.fragment.IBaseVmFragment
import com.mvvm.ktnet.base.viewmodel.BaseViewModel
import com.mvvm.ktnet.base.viewmodel.LoadingType
import com.mvvm.ktnet.ext.loadServiceInit
import com.mvvm.ktnet.ext.showEmpty
import com.mvvm.ktnet.ext.showError
import com.mvvm.ktnet.ext.showLoading
import com.scwang.smart.refresh.layout.SmartRefreshLayout


/**
 *
 * @ProjectName:    KtNet
 * @Package:        com.mvvm.ktnet.base.myappbase
 * @ClassName:      BaseFragment
 * @Description:     java类作用描述
 * @Author:         Alex
 * @CreateDate:     2020/11/12 11:08
 * @UpdateUser:     更新者：
 * @UpdateDate:     2020/11/12 11:08
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */
abstract class BaseDbFragment<VM : BaseViewModel, DB : ViewDataBinding> : IBaseVmDbFragment<VM, DB>() {


    //界面状态管理者
    private var loadsir: LoadService<Any>? = null

    private var loadView: View? = null


    override fun initLoad() {

        loadView = view?.findViewById(R.id.load_root)

        loadsir = loadView?.let {
            loadServiceInit(it) {
                //重试操作
                reLoad()
            }
        }
    }


    var smartRefreshLayout: SmartRefreshLayout? = null


    override fun initRefreshLayout() {
        smartRefreshLayout = view?.findViewById<SmartRefreshLayout>(R.id.smartRefreshLayout)
        smartRefreshLayout?.apply {

        }
    }


    //这里显示dialo等
    override fun registerUiChange() {
        super.registerUiChange()

        dialogObserve(mViewModel)
    }


    fun dialogObserve(viewModel: BaseViewModel) {
        //显示弹窗
        viewModel.loadingChange.showPageLoad.observe(this, Observer {

            when (it.loadingType) {
                LoadingType.LOADING_PAGE -> {
                    when (it.show) {
                        true -> {
                            if (it.empty && !it.error) {
                                showEmpty()
                            } else if (!it.empty && it.error) {
                                showError()
                            } else if (!it.empty && !it.error) {
                                showLoading()
                            }
                        }
                        else -> {
                            showSuccess()
                        }
                    }


                }
                LoadingType.LOADINGDIALOG -> {
                    if (it.show) {
                        if (it.empty || it.error) dismissDialogLoading() else
                            showDialogLoading(it.loadString)
                    } else dismissDialogLoading()
                }

            }
        })
    }


    override fun addLoadingObserve(vararg viewModels: BaseViewModel) {
        super.addLoadingObserve(*viewModels)
        viewModels.forEach { viewModel ->
            //显示弹窗
            dialogObserve(viewModel)
        }
    }


    override fun registorDefErrorChange() {

        mViewModel.loadStatus.loadEmpty.observe(this, Observer {
            onEmpty(it.mathCode, it.throwable)
        })

        mViewModel.loadStatus.loadError.observe(this, Observer {

            onError(it.mathCode, it.throwable)

        })
        mViewModel.loadStatus.loadFinally.observe(this, Observer {

            onFinally(it.mathCode)
        })

    }
    override fun secondLoad() {

    }
    override fun reLoad() {

    }

    override fun onError(matcCode: Int, throwable: Throwable) {


    }

    override fun onEmpty(matcCode: Int, throwable: Throwable) {


    }

    override fun onFinally(matcCode: Int) {

        smartRefreshLayout?.apply {
            finishLoadMore()
            finishRefresh()
        }
    }

    override fun showLoading(message: String) {
        loadsir?.showLoading()
    }

    override fun showError(message: String) {
        loadsir?.showError()
    }

    override fun showEmpty(message: String) {
        loadsir?.showEmpty()
    }

    override fun showSuccess(message: String) {
        loadsir?.showSuccess()
    }


    private var alertDialog: AlertDialog? = null

    override fun showDialogLoading(message: String) {

        if (alertDialog == null) {
            alertDialog = AlertDialog.Builder(requireActivity()).create().apply {
                window?.setBackgroundDrawable(ColorDrawable())
                setCancelable(false)
                setContentView(R.layout.loading_alert)
                setCanceledOnTouchOutside(false)
            }

        }
        if (alertDialog?.isShowing == false)
            alertDialog?.show()
    }

    override fun showDialogError(message: String) {
        alertDialog?.dismiss()

    }

    override fun showDialogEmpty(message: String) {
        alertDialog?.dismiss()
    }

    override fun dismissDialogLoading() {
        alertDialog?.dismiss()
    }
}